<template>
  <transition name="slide">
    <div v-show="showFlag" class="select_food" id="select_food">
      <div class="food_content">
        <!-- 图片区-->
        <div class="header_image">
          <img :src="selectedFood.image" alt="foodimage">
          <div class="back" @click="hide">
            <span class="iconfont">&#xe601;</span>
          </div>
        </div>
        <!--食物详情-->
        <div class="food_detail">
          <h1 class="food_title">{{selectedFood.name}}</h1>
          <p class="food_desc">
            <span class="sell_count">月售{{selectedFood.sellCount}}份</span><span class="sell_rating">
            好评率 {{selectedFood.rating}}%</span>
          </p>
          <div class="food_price">
            <span class="food_nowPrice">¥{{selectedFood.price}}</span>
            <span class="food_oldPrice" v-show="selectedFood.oldPrice">¥{{selectedFood.oldPrice}}</span>
          </div>
          <!--按钮区-->
          <div class="cartcontrol_wrapper" v-show="selectedFood.count > 0">
            <v-cartcontrol :food="selectedFood"></v-cartcontrol>
          </div>
          <div class="buy" @click.stop.prevent="addFood($event)" v-show="!selectedFood.count || selectedFood.count ===0">
            加入购物车
          </div>
        </div>
        <!--切割层-->
        <v-split v-show="selectedFood.info"></v-split>
        <!--食物介绍-->
        <div class="food_info" v-show="selectedFood.info">
          <h1>商品介绍</h1>
          <p class="food_text">
            {{selectedFood.info}}
          </p>
        </div>
        <!--切割层-->
        <v-split></v-split>
        <!--食物评价-->
        <div class="food_rating">
          <h1 class="title">商品评价</h1>
          <v-ratingselect :select-type="selectType" :only-content="onlyContent" :select-desc="selectDesc"
                          :ratings="selectedFood.ratings" v-on:select-type="changeSelect" v-on:content-toggle="changeContent">
          </v-ratingselect>
          <!--评价的内容 -->
          <div class="rating_wrapper">
            <ul v-show="selectedFood.ratings && selectedFood.ratings.length">
              <li v-for="rating in selectedFood.ratings" v-show="needShow(rating.rateType, rating.text)" class="rating_item">
                <div class="user fr">
                  <span class="name" v-text="rating.username"></span>
                  <img width="12" height="12" :src="rating.avatar" alt="userImage" class="avatar">
                </div>
                <div class="time">{{rating.rateTime | formate('YYYY-MM-DD hh:mm')}}</div>
                <p class="text">
                  <i class="iconfont" :class="[rating.rateType===0?'up':'down']">{{rating.rateType===0?'&#xe608;':'&#xe7b0;'}}</i>
                  {{rating.text}}
                </p>
              </li>
            </ul>
            <div class="no_rating" v-show="!selectedFood.ratings || !selectedFood.ratings.length">
              暂无相关评价
            </div>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  import Vue from 'vue'
  import BScroll from 'better-scroll'
  import cartcontrol from 'components/cartcontrol/cartcontrol'
  import split from 'components/split/split'
  import ratingselect from 'components/ratingselect/ratingselect'
  //import format from 'date-fns/format'

  const All = 2
  var Types = {'Types': All} /* 选择的评价的类型 */
  var onlyContent = {'onlyContent': false}
  export default {
    data() {
      return {
        showFlag: false,
        selectType: Types,
        onlyContent: onlyContent,
        selectDesc: {
          all: '全部',
          positive: '推荐',
          negative: '吐槽'
        }
      }
    },
    components: {
      'v-split': split,
      'v-cartcontrol': cartcontrol,

      'v-ratingselect': ratingselect
    },
    props: {
      selectedFood: Object
    },
    methods: {
      show() {
        this.showFlag = true
        this.selectType = Types
        this.onlyContent = onlyContent
        this.$nextTick(() => {
          if(!this.scroll) {
            this.scroll = new BScroll(document.getElementById('select_food'), {
              click: true
            })
          }else {
            this.scroll.refresh()
          }
        })
      },
      hide() {
        this.showFlag = false
      },
      addFood(event) {
        if(!event._constructed) {
          return
        }
        Vue.set(this.selectedFood, 'count', 1)
      },
      needShow(type, text) {
        if(this.onlyContent.onlyContent && !text) {
          return false
        }
        if(this.selectType.Types === All) {
          return true
        }else {
          return type === this.selectType.Types
        }
      },
      changeSelect: function (type) {
        this.$nextTick(() => {
          this.scroll.refresh()
        })
      },
      changeContent(content) {
        this.$nextTick(() => {
          this.scroll.refresh()
        })
      }
    }/*,
    filters: {
      formate(time, opts) {
        return format(time, opts)
      }
    }*/
  }
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  .select_food {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 48px;
    z-index: 30;
    width: 100%;
    background-color: #e5eef5;
    .food_content {
      /*图片区*/
      .header_image {
        position: relative;
        width: 100%;
        height: 0;
        padding-top: 100%;
        img {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
        }
        .back {
          position: absolute;
          left: 5px;
          top: 10px;
          .iconfont {
            display: block;
            padding: 10px;
            font-size: 20px;
            color: rgba(190, 194, 197, 0.77);
          }
        }
      }
      /*食物详情*/
      .food_detail {
        position: relative;
        padding: 18px;
        .food_title {
          font-size: 14px;
          font-weight: 700;
          color: rgb(7,17,27);
          line-height: 14px;
          margin-bottom: 8px;
        }
        .food_desc {
          margin-bottom: 18px;
          line-height: 10px;
          color: rgb(147,153,159);
          font-size: 10px;
          .sell_count {
            margin-right: 8px;
          }
        }
        .food_price {
          font-weight: 700;
          line-height: 24px;
          .food_nowPrice {
            margin-right: 4px;
            font-size: 14px;
            color: #F01212;
          }
          .foof_oldPrice {
            font-size: 10px;
            color: #8F969C;
            text-decoration: underline;
          }
        }
        .cartcontrol_wrapper {
          position: absolute;
          right: 12px;
          bottom: 12px;
        }
        .buy {
          position: absolute;
          right: 18px;
          bottom: 18px;
          z-index: 10;
          height: 24px;
          line-height: 24px;
          padding: 0 12px;
          font-size: 10px;
          border-radius: 12px;
          color: #fff;
          background-color: #00A0DC;
          &:active {
            background-color: #4488a2;
          }
        }
      }
      /*食物介绍*/
      .food_info {
        padding: 18px;
        h1 {
          line-height: 14px;
          margin-bottom: 6px;
          font-size: 14px;
          font-weight: 600;
          color: rgb(7,17,27);
        }
        .food_text {
          padding-left: 8px;
          font-size: 12px;
          font-weight: 200;
          color: rgb(77,85,93);
          line-height: 24px;
        }
      }
      .food_rating {
        padding-top: 18px;
        .title {
          line-height: 14px;
          padding-left: 18px;
          margin-bottom: 6px;
          font-size: 14px;
          font-weight: 600;
          color: rgb(7,17,27);
        }
        .rating_wrapper {
          padding: 0 18px;
          .rating_item {
            padding: 15px 0;
            border-bottom: 1px solid rgba(7,17,27,0.3);
            .user {
              font-size: 0;
              line-height: 12px;
              .name {
                display: inline-block;
                font-size: 10px;
                color: rgb(147, 153, 159);
                margin-right: 6px;
                vertical-align: top;
              }
              .avatar {
                display: inline-block;
                width: 12px;
                height: 12px;
                border-radius: 50%;
              }
            }
            .time {
              line-height: 12px;
              font-size: 10px;
              color: rgb(147, 153, 159);
            }
            .text {
              line-height: 16px;
              margin-top: 2px;
              font-size: 12px;
              color: rgb(7, 17, 27);
              .iconfont {
                font-size: 12px;
                line-height: 24px;
                margin-right: 2px;
                &.up {
                  color: rgb(0, 100, 220);
                }
                &.down {
                  color: rgb(147, 153, 159);
                }
              }
            }
          }
          .no_rating {
            padding: 16px 0;
            font-size: 12px;
            color: rgb(147, 153, 159);
          }
        }
      }
    }
  }


  .slide-enter-active, .slide-leave-active {
    transition: all 0.35s;
  }
  .slide-enter, .slide-leave-active {
    transform: translate3d(100%, 0, 0);
  }
</style>